// models/medicalRecord.js
import { Model, DataTypes } from 'sequelize';
import sequelize from '../config/db.js';
import User from './user.js';

class MedicalRecord extends Model { }
MedicalRecord.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    hospital: {
      type: DataTypes.STRING(255),
      allowNull: true,
      validate: {
        len: { args: [0, 255], msg: '医院名称长度不能超过255字符' }
      }
    },
    // 科室
    department: {
      type: DataTypes.STRING,
    },
    // 诊断结果
    diagnosis: {
      type: DataTypes.STRING,
    },
    // 医嘱
    advice: {
      type: DataTypes.STRING,
    },
    // 时间
    time: {
      type: DataTypes.DATE,
    },
    // status: {
    //   type: DataTypes.BOOLEAN,
    //   defaultValue: true,
    // },
  },
  {
    sequelize,
    modelName: 'MedicalRecord',
    tableName: 'medicalRecords',
  }
);

// 建立关系
MedicalRecord.belongsTo(User, { foreignKey: 'userId', as: 'user' });
User.hasMany(MedicalRecord, { foreignKey: 'userId', as: 'medicalRecords' });

export default MedicalRecord;